package main

import "fmt"
import "time"
import "bytes"
import "encoding/base64"
import "io"
import "os"

/*
func main() {
	c1 := make(chan string, 1)
	go func() {
		time.Sleep(time.Second * 100)
		c1 <- "result 1"

	}()
	select {
	case res := <-c1:
		fmt.Println(res)
	case <-time.After(time.Second * 60):
		fmt.Println("timeout 1")
	}
}
*/

func worker(id int, jobs chan int, results chan int) {
	for j := range jobs {
		fmt.Println("worker", id, "process job", j)
		time.Sleep(time.Second)
		results <- j * 2
	}
}

func sumResult(results chan int) {
	var sum int
	for a := 1; a <= 9; a++ {
		s := <-results
		sum += s
		fmt.Println("sum", sum, "s", s)
	}
}
func main() {
	jobs := make(chan int, 100)
	results := make(chan int, 100)
	for w := 1; w <= 3; w++ {
		go worker(w, jobs, results)
	}
	for j := 1; j <= 9; j++ {
		jobs <- j
	}
	close(jobs)
	sumResult(results)
	buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==")
	dec := base64.NewDecoder(base64.StdEncoding, buf)
	io.Copy(os.Stdout, dec)
}
